package Tree;

/**
 * @Classname JZ26树的子结构
 * @Description TODO
 * @Date 2023/2/20 22:37
 * @Created by xjl
 */
public class JZ26树的子结构 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    /**
     * @description 判断一个树是否为一个树的子树
      * @param: A
     * @param: B
     * @date: 2023/2/20 22:40
     * @return: boolean
     * @author: xjl
    */
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        return (A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
    }

    boolean recur(TreeNode A, TreeNode B) {
        if(B == null) {return true;}
        if(A == null || A.val != B.val) {return false;}
        return recur(A.left, B.left) && recur(A.right, B.right);
    }
}
